
package com.wwy.monitor.controller;

import java.io.IOException;
import java.util.Date;

import com.wwy.monitor.common.ServiceResponse;
import com.wwy.monitor.model.enumeration.AlertTypeEnum;
import com.wwy.monitor.model.enumeration.ComparatorTypeEnum;
import com.wwy.monitor.model.tddl.dto.MonitorConfigDTO;
import com.wwy.monitor.model.enumeration.ReceiverTypeEnum;
import com.wwy.monitor.model.tddl.query.MonitorConfigQuery;
import com.wwy.monitor.service.MonitorConfigService;
import com.wwy.monitor.util.UserUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/monitor")
public class MonitorConfigController {
    @Resource
    private MonitorConfigService monitorConfigService;

    /**
     * 获取全部配置类型
     */
    @GetMapping("/monitorConfigs")
    @ResponseBody
    private ServiceResponse getAllConfig() {
        return ServiceResponse.success(monitorConfigService.getAllConfig());
    }

    /**
     * 分页查询配置
     */
    @GetMapping("/config/page")
    @ResponseBody
    private ServiceResponse page(MonitorConfigQuery param) {
        return ServiceResponse.success(monitorConfigService.queryMonitorConfig(param));
    }

    /**
     * 获取全部操作类型
     */
    @GetMapping("/comparators")
    @ResponseBody
    private ServiceResponse getAllComparator() {
        return ServiceResponse.success(ComparatorTypeEnum.toEnumList());
    }

    /**
     * 新增监控配置
     */
    @PostMapping("/add")
    public ServiceResponse addMonitorConfig(@RequestBody MonitorConfigDTO dto, HttpServletRequest request) throws IOException, ServletException {
        dto.setCreateTime(new Date());
        dto.setCreateUser(UserUtils.getBucEmpId(request));
        return ServiceResponse.success(monitorConfigService.addMonitorConfig(dto));
    }

    /**
     * 逻辑删除监控配置
     */
    @PostMapping("{id}/delete")
    public ServiceResponse delete(@PathVariable Long id, HttpServletRequest request) throws IOException, ServletException {
        return ServiceResponse.success(monitorConfigService.deleteMonitorConfig(id, UserUtils.getBucUser(request)));
    }

    /**
     * 获取全部报警类型
     */
    @GetMapping("/alertTypes")
    @ResponseBody
    private ServiceResponse getAllAlertType() {
        return ServiceResponse.success(AlertTypeEnum.toEnumList());
    }

    /**
     * 获取全部报警接受者类型
     */
    @GetMapping("/receiverTypes")
    @ResponseBody
    private ServiceResponse getAllReceiverType() {
        return ServiceResponse.success(ReceiverTypeEnum.toEnumList());
    }
}